# coding:utf-8

'''
http工具类

'''
from urllib import request
from urllib import parse
import ssl

headers = {
    # heard部分直接通过chrome部分request header部分
    'Accept': 'application/json, text/plain, */*',
    'Accept-Encoding': 'gzip, deflate',
    'Accept-Language': 'zh-CN,zh;q=0.8',
    'Connection': 'keep-alive',
    'Content-Length': '14',  # get方式提交的数据长度，如果是post方式，转成get方式：【id=wdb&pwd=wdb】
    'Content-Type': 'application/x-www-form-urlencoded',
    'Referer': 'http://10.1.2.151/',
    'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.23 Mobile Safari/537.36'

}


# GET方法
def http_get_request(url_name):
    return request.urlopen(request.Request(url_name)).read().decode('utf-8')


# GET方法
def https_get_request(url_name):
    context = ssl._create_unverified_context()
    return request.urlopen(request.Request(url_name), context=context).read().decode('utf-8')


# POS
'''
url = "http://www.baidu.com"
data = {"id": "wdb", "pwd": "wdb"}
params = "?"
for key in data:
    params = params + key + "=" + data[key] + "&"
print("Get方法参数：" + params)
'''


def post_request(url_name, data):
    data = parse.urlencode(data).encode('utf-8')
    req = request.Request(url_name, headers=headers, data=data)
    page = request.urlopen(req).read()
    return page.decode('utf-8')


if __name__ == "__main__":
    print(https_get_request("https://114.116.47.107:7001/app/v1/model/list/1/10"))
